package com.lvhx.springboot.springbatch.controller;

import com.lvhx.facade.RunJobService;
import com.lvhx.facade.dto.RunJobRequest;
import com.lvhx.springboot.dynamicdatasource.DataSource;
import com.lvhx.springboot.entity.User;
//import com.lvhx.springboot.entity.batch.BatchJobGroupConfig;
//import com.lvhx.springboot.entity.batch.BatchJobGroupConfigExample;
//import com.lvhx.springboot.mapper.batch.BatchJobGroupConfigMapper;
import com.lvhx.springboot.service.UserService;
import org.springframework.batch.core.*;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @auther 吕宏旭
 * @date 2020/6/5 17:53
 */
@RestController
@RequestMapping("/order")
public class BatchController {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private UserService userService;

//    @Autowired
//    private BatchJobGroupConfigMapper batchJobGroupConfigMapper;

    @Autowired
    private RunJobService runJobService;

    @GetMapping("/batch")
    public void aaa(){
        JobLauncher jobLauncher = (JobLauncher) applicationContext.getBean("jobLauncher");
        Job job = (Job) applicationContext.getBean("helloWordJob");
        try {

            JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();

            JobParameters jobParameters = jobParametersBuilder.addDate("date",new Date()).toJobParameters();

            JobExecution result = jobLauncher.run(job,jobParameters);

            System.out.println(result.toString());
        } catch (JobExecutionAlreadyRunningException e) {
            e.printStackTrace();
        } catch (JobRestartException e) {
            e.printStackTrace();
        } catch (JobInstanceAlreadyCompleteException e) {
            e.printStackTrace();
        } catch (JobParametersInvalidException e) {
            e.printStackTrace();
        }
    }

    @GetMapping(value = {"/dynamicdatasource"})
    @DataSource(value = "master")
    public User testDynamicDataSource(){
        //不用注解形式，直接set
//        DynamicDataSourceContextHolder.setDataSourceKey("slave");
        User user = userService.findById(2L);
//        UserExample example = new UserExample();
//        example.createCriteria().andIdEqualTo(1L);
//        List<User> list = userMapper.selectByExample(example);
        return user;
    }

    @GetMapping(value = {"/runGroupABatch"})
    public void runGroupBatch(){
//        BatchJobGroupConfigExample example = new BatchJobGroupConfigExample();
//        example.createCriteria().andGroupIdEqualTo("A");
//        List<BatchJobGroupConfig> list = batchJobGroupConfigMapper.selectByExample(example);
//        for (BatchJobGroupConfig batchJobGroupConfig : list){
//            RunJobRequest request = new RunJobRequest();
//            request.setPostDate(new Date());
//            request.setJobName("helloWordJob");
//        }
    }

}
